﻿//creare namespace
Ext.ns('Portal.companyScreen');

Ext.apply(Portal.companyScreen, {

    //functie de stergere / editare inregistrare
    action: function () {
        var oGrid, iRec = 0, oWind, op = "edit";

        oGrid = Ext.getCmp(Portal.companyScreen.id).getComponent("companyGrid");

        //setare tip operatie; daca este butonul de adaugare / editare atunci se citeste paramentrul `action` al acestuia
        if (this.action) {
            op = this.action;
        }

        if (op === "edit") {
            if (oGrid.selModel.getCount() != 1) {
                Portal.events.throwInformation("Selectati o inregistrare !");
                return;
            }
            iRec = oGrid.selModel.getSelected().id;
        }

        //constructie formular companii
        var oFpAdd = new Ext.FormPanel({
            autoHeight: true, //inaltimea formului va da inaltimea ferestrei
            labelWidth: 160,
            frame: true,
            bodyStyle: {
                padding: "5px"
            },
            layout: 'form',
            defaults: {
                anchor: '90%',
                xtype: "textfield",
                allowBlank: false
            },
            items: [
                {
                    name: 'CompanyName',
                    fieldLabel: 'Companie'
                },
                {
                    name: 'Cui',
                    fieldLabel: 'CUI'
                },
                {
                    fieldLabel: 'Cifra afaceri',
                    xtype: "uxFCombo",
                    anchor: "70%",
                    name: "IdTurnover",
                    itemId: "turnover",
                    forceSelection: false,
                    minChars: 2,
                    easyConfig: {
                        baseParams: {
                            table_name: 'turnover'
                        },
                        proxyUrl: 'Nom/GetNom/'
                    }
                },
                {
                    fieldLabel: 'Nr. Angajati',
                    xtype: "uxFCombo",
                    anchor: "70%",
                    name: "IdEmployee",
                    itemId: "employee",
                    forceSelection: false,
                    minChars: 2,
                    easyConfig: {
                        baseParams: {
                            table_name: 'employee'
                        },
                        proxyUrl: 'Nom/GetNom/'
                    }
                },
                {
                    name: "Id",
                    xtype: "hidden",
                    value: iRec
                }
            ],
            buttons: [
                {
                    text: 'Salvare',
                    handler: function () {

                        if (!oFpAdd.getForm().isValid()) {
                            return;
                        }

                        oFpAdd.getForm().submit({
                            url: "Company/EditCompany",
                            success: function (form, response) {
                                oGrid.store.reload();
                                oWind.close();
                            }
                        });
                    }
                }
            ]
        });

        //constructie fereastra
        oWind = new Ext.Window({
            title: (op === "edit") ? "Editare inregistrare" : "Adaugare companie",
            width: 450,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            items: [oFpAdd]
        }).show();

        //daca este editare de inregistrare atunci se aduc datele acestei inregistrari
        if (op === "edit") {
            oFpAdd.on("afterlayout", function () {
                        Ext.Ajax.request({
                            url: 'Company/GetCompanyById',
                            params: {
                                Id: iRec
                            },
                            success: function (response) {
                                var oRes = Ext.decode(response.responseText);
                                var oData = oRes.Records;
                                fnCompleteForm(oFpAdd, oData[0]);  //vezi utils.js
                            }
                        })
                    }, this, { single: true });
        }
    },


    //functie de stergere / editare inregistrare
    action2: function () {
        var oGrid, cGrid, iRec = 0, oWind, op = "edit";

        oGrid = Ext.getCmp(Portal.companyScreen.id).getComponent("branchGrid");
        cGrid = Ext.getCmp(Portal.companyScreen.id).getComponent("companyGrid");

        //setare tip operatie; daca este butonul de adaugare / editare atunci se citeste paramentrul `action` al acestuia
        if (this.action) {
            op = this.action;
        }

        if (op === "edit") {
            if (oGrid.selModel.getCount() != 1) {
                Portal.events.throwInformation("Selectati o inregistrare !");
                return;
            }
            iRec = oGrid.selModel.getSelected().id;
        }

        iComp = cGrid.selModel.getSelected().id;

        //constructie formular  branch
        var oFpAdd = new Ext.FormPanel({
            autoHeight: true, //inaltimea formului va da inaltimea ferestrei
            labelWidth: 160,
            frame: true,
            bodyStyle: {
                padding: "5px"
            },
            layout: 'form',
            defaults: {
                anchor: '90%',
                xtype: "textfield",
                allowBlank: false
            },
            items: [
                {
                    name: 'BranchName',
                    fieldLabel: 'Sucursala'
                },
                {
                    fieldLabel: 'Oras',
                    xtype: "uxFCombo",
                    name: "IdCity",
                    itemId: "IdCity",
                    forceSelection: false,
                    minChars: 2,
                    easyConfig: {
                        baseParams: {
                            table: "nomcity"
                        },
                        proxyUrl: 'City/GetAllCity/'
                    }
                },
                {
                    name: 'Country',
                    fieldLabel: 'Tara'
                },
                {
                    name: 'Address',
                    fieldLabel: 'Adresa'
                },
                {
                    name: 'AddressDetail',
                    fieldLabel: 'Reper adresa'
                },
                {
                    name: "Id",
                    xtype: "hidden",
                    value: iRec
                },
                {
                    name: "IdComp",
                    xtype: "hidden",
                    value: iComp
                }
            ],
            buttons: [
                {
                    text: 'Salvare',
                    handler: function () {

                        if (!oFpAdd.getForm().isValid()) {
                            return;
                        }

                        oFpAdd.getForm().submit({
                            url: "Branch/EditBranch",
                            success: function (form, response) {
                                oGrid.store.reload();
                                oWind.close();
                            }
                        });
                    }
                }
            ]
        });

        //constructie fereastra
        oWind = new Ext.Window({
            title: (op === "edit") ? "Editare inregistrare" : "Adaugare sucursala",
            width: 450,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            items: [oFpAdd]
        }).show();

        //daca este editare de inregistrare atunci se aduc datele acestei inregistrari
        if (op === "edit") {
            oFpAdd.on("afterlayout", function () {
                        Ext.Ajax.request({
                            url: 'Branch/GetBranchById',
                            params: {
                                Id: iRec
                            },
                            success: function (response) {
                                var oRes = Ext.decode(response.responseText);
                                var oData = oRes.Records;
                                fnCompleteForm(oFpAdd, oData[0]);  //vezi utils.js
                            }
                        })
                    }, this, { single: true });
        }
    }


});

Ext.apply(Portal.companyScreen, {

    id: 'idTabCompanyScreen',
    exec: function () {

        if (!Ext.getCmp(Portal.companyScreen.id)) {

            var oCmCompany = new Ext.grid.ColumnModel({
                columns: [
                    {
                        header: "Nume",
                        dataIndex: 'CompanyName',
                        width: 100
                    },
                    {
                        header: "CUI",
                        dataIndex: 'Cui',
                        width: 100
                    },
                    {
                        header: "Cifra Afaceri",
                        dataIndex: 'TurnoverName',
                        width: 100
                    },
                    {
                        header: "Nr. Angajati",
                        dataIndex: 'EmployeeName'
                    }
                ],
                defaults: {
                    sortable: true
                }
            });

            var oCmBranch = new Ext.grid.ColumnModel({
                columns: [
                    {
                        header: "Nume",
                        dataIndex: 'BranchName',
                        width: 100
                    },
                    {
                        header: "Oras",
                        dataIndex: 'CityName',
                        width: 100
                    },
                    {
                        header: "Judet",
                        dataIndex: 'CountyName',
                        width: 100
                    },
                    {
                        header: "Tara",
                        dataIndex: 'Country'
                    },
                    {
                        header: "Adresa",
                        dataIndex: 'Address'
                    },
                    {
                        header: "Reper adresa",
                        dataIndex: 'AddressDetail'
                    }
                ],
                defaults: {
                    sortable: true
                }
            });

            var oCompanyGrid = new Ext.ux.fastGrid({
                xtype: "uxFGrid",
                layout: 'fit',
                flex: 1,
                title : "Companii",
                itemId: "companyGrid",
                gridConfig: {
                    url: 'Company/GetCompany',
                    fields: ['IdCompany', 'CompanyName', 'Cui', 'TurnoverName', 'EmployeeName']
                },
                cm: oCmCompany,
                tbar: [
                    {
                        text: "Adauga",
                        iconCls: 'icon-add',
                        action: "add",
                        handler: Portal.companyScreen.action
                    },
                    "-",
                    {
                        text: "Editeaza",
                        iconCls: 'icon-cog-edit',
                        action: "edit",
                        handler: Portal.companyScreen.action
                    }
                ],
                listeners: {
                    'rowdblclick': Portal.companyScreen.action,
                    'rowclick': function () {
                        var selectedRowId;
                        if (oCompanyGrid.selModel.getCount() == 1) {
                            selectedRowId = oCompanyGrid.selModel.getSelected().id;
                            oBranchGrid.store.reload({ params: { companyId: selectedRowId} });
                        }
                    }
                }

            });


            var oBranchGrid = new Ext.ux.fastGrid({
                xtype: "uxFGrid",
                layout: 'fit',
                flex: 1,
                title : "Sucursale",
                itemId: "branchGrid",
                gridConfig: {
                    autoLoad : false,
                    url: 'Branch/GetBranch',
                    fields: ['IdBranch', 'BranchName', 'CityName', 'CountyName', 'Country','Address','AddressDetail'],
                    viewConfig: {
                        getRowClass: function (record, index) {

                        }
                    }
                },
                cm: oCmBranch,
                tbar: [
                    {
                        text: "Adauga",
                        iconCls: 'icon-add',
                        action: "add",
                        handler: Portal.companyScreen.action2
                    },
                    "-",
                    {
                        text: "Editeaza",
                        iconCls: 'icon-cog-edit',
                        action: "edit",
                        handler: Portal.companyScreen.action2
                    }
                ],
                listeners: {
                    'rowdblclick': Portal.companyScreen.action2
                }
            });

            //creare tab ce contine gridurile
            var oNomTab = new Ext.Panel({
                title: 'Companii',
                id: Portal.companyScreen.id,
                iconCls: 'icon-fugue-block',
                closable: true,
                layout: {
                    type: 'hbox',
                    pack: 'start',
                    align: 'stretch'
                },
                items: [oCompanyGrid, oBranchGrid]
            });

            //adaugare tab la tabpanel-ul principal
            Portal.oCenterRegion.add(oNomTab);
            Portal.oCenterRegion.doLayout();

        }
        //activare tab
        Portal.oCenterRegion.setActiveTab(Portal.companyScreen.id);
    }
});